//
//  LineKeepViewController.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/7/25.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "LineKeepViewController.h"
#import "Common.h"
#import "ActionSheetPicker.h"
#import "area.h"
#import "TopBackView.h"


#define MySetValue(dict, value) ((!dict[value] || [dict[value] isKindOfClass:[NSNull class]])?@"":dict[value])


@interface LineKeepViewController ()
{
    area * AREA;

}

@property (nonatomic,strong) UIButton *startButton;
@property (nonatomic,strong) UIButton *endButton;
@property (nonatomic,strong) NSMutableDictionary *postDic;

@property (nonatomic, strong) NSMutableArray *provienceArray;
@property (nonatomic, strong) NSArray *cityArray;
@property (nonatomic) NSString *btnString;

@end

@implementation LineKeepViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"新增路线"];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

    
    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topBackView];
    self.view.backgroundColor = GrayColor;
    
    [self initSubViews];
}

-(void)initSubViews
{
    self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.endButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIView *firstV = [self backViewWithString:@"起运地" andButton:self.startButton andFloat:64];
    
    [self.view addSubview:firstV];
    
    UIView *secondV = [self backViewWithString:@"目的地" andButton:self.endButton andFloat:64 + cellHeight];
    [self.view addSubview:secondV];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(18, CGRectGetMaxY(secondV.frame) + 30, screenWidth - 36, Button_height);
//    addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    addButton.backgroundColor = YellowColor;
    addButton.layer.cornerRadius = 5;
    [addButton setTitle:@"保存" forState:UIControlStateNormal];
    [addButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(saveButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];


}

-(UIView *)backViewWithString:(NSString *)string andButton:(UIButton *)button andFloat:(CGFloat)y
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, y, screenWidth, cellHeight)];
    view.backgroundColor = WhiteColor;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 50, cellHeight)];
    label.text = string;
    label.font = Font(13);
    label.textColor = fontGrayColor;
    [view addSubview:label];
    
    button.frame = CGRectMake(CGRectGetMaxX(label.frame), 0, 300, cellHeight);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.titleLabel.font = Font(13);
    button.titleLabel.textColor = littleBlackColor;
    button.tag = y + 100;
    [button addTarget:self action:@selector(buttonGetPosition:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:button];
    
    return view;

}

//保存
-(void)saveButton
{
    
}

//获取地址
-(void)buttonGetPosition:(UIButton *)sender
{
    if (sender.tag > 200) {
        _btnString = @"2";
    } else {
        _btnString = @"1";
    }
    [self pressBtnArea];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)pressBtnArea{
    

    _provienceArray = nil;
    _cityArray = nil;
    
    _provienceArray = [AREA selectAreaWithFArea:nil withTarea:nil withFarea:1];
    _cityArray = [AREA selectAreaWithFArea:@"北京市" withTarea:nil withFarea:2];
    //    _areaArray = [AREA selectAreaWithFArea:@"北京市" withTarea:@"市辖区" withFarea:3];
    
    
    UIImageView * image = (UIImageView*)[self.view viewWithTag:1000];
    image.transform = CGAffineTransformMakeRotation(M_PI_2);
    UIPickerView *education = [[UIPickerView alloc] init];
    ActionSheetPicker *actionSheet=[[ActionSheetPicker alloc]initWithTarget:self successAction:@selector(selectOK:) cancelAction:nil origin:education];
    
    CGRect pickerFrame = CGRectMake(0, 40, actionSheet.viewSize.width, 216);
    [education setFrame:pickerFrame];
    education.delegate = self;
    education.dataSource = self;
    //pickview  左选择按钮的文字
    UIButton *leftBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消"forState:UIControlStateNormal];
    leftBtn.frame=CGRectMake(0, 0, 50, 45);
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *leftIte=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    //pickview    右选择按钮的文字
    UIButton *rightBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    rightBtn.frame=CGRectMake(0, 0, 50, 45);
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightIte=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    //将按钮添加到pickerview 上
    [actionSheet setDoneButton:rightIte];
    [actionSheet setCancelButton:leftIte];
    actionSheet.pickerView = education;
    actionSheet.title = @"地址选择";
    [actionSheet showActionSheetPicker];
    NSLog(@"点击的是地区选择");
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    NSInteger ss = [pickerView selectedRowInComponent:0];
    NSInteger dd = [pickerView selectedRowInComponent:1];
    
    switch (component) {
        case 0:
        {
            NSDictionary *dict = [_provienceArray objectAtIndex:row];
            NSString *code = MySetValue(dict, @"provinceName");
            if (code.length > 0)
            {
                _cityArray = [AREA selectAreaWithFArea:code withTarea:nil withFarea:2];
                [pickerView reloadComponent:1];
                NSDictionary *cityDict = [_cityArray objectAtIndex:dd];
                NSString * cityFcode = MySetValue(cityDict, @"cityName");
                if (cityFcode.length > 0)
                {
                    //                    _areaArray = [AREA selectAreaWithFArea:code withTarea:cityFcode withFarea:3];
                    //                    [pickerView reloadComponent:2];
                }
            }
        }
            break;
        case 1:
        {
            NSDictionary *dict = [_cityArray objectAtIndex:row];
            NSString *code = MySetValue(dict, @"cityName");
            
            NSDictionary * dics = [_provienceArray objectAtIndex:ss];
            NSString * str = MySetValue(dics, @"provinceName");
            
            if (code.length > 0) {
                //                _areaArray = [AREA selectAreaWithFArea:str withTarea:code withFarea:3];
                //                [pickerView reloadComponent:2];
            }
        }
            break;
        case 2:{
            
        }
            break;
            
        default:
            break;
    }
}


- (void)selectOK:(id)sender{
    
    
    UIPickerView *pick = sender;
    NSInteger row = [pick selectedRowInComponent:0];
    NSDictionary *dict = [_provienceArray objectAtIndex:row];
    NSString *provinceName = MySetValue(dict, @"provinceName");//省名称
    NSString *provinceCode = MySetValue(dict, @"provinceCode");//省编码
    
    
    NSInteger row1 = [pick selectedRowInComponent:1];
    NSDictionary *dict1 = [_cityArray objectAtIndex:row1];
    NSString *cityName = MySetValue(dict1, @"cityName");//市名称
    NSString *citycode = MySetValue(dict1, @"cityCode");//市编码
    
    //    NSInteger row2 = [pick selectedRowInComponent:2];
    //    NSDictionary *dict2 = [_areaArray objectAtIndex:row2];
    //    NSString *countyName = MySetValue(dict2, @"countyName");//县名称
    //    NSString * countycode = MySetValue(dict2, @"countyCode");//县编码
    
    
    [self.postDic setObject:provinceCode forKey:@"provincecode"];//省编码
    [self.postDic setObject:provinceName forKey:@"provincename"]; //省名称
    [self.postDic setObject:citycode forKey:@"citycode"];//市编码
    [self.postDic setObject:cityName forKey:@"cityname"]; //市名称
    //    [dic setObject:countycode forKey:@"countycode"];//县编码
    //    [dic setObject:countyName forKey:@"countyname"]; //县名称
    
    NSString *provinceName1 = [NSString stringWithFormat:@"%@",provinceName];
    NSString *cityName1 = [NSString stringWithFormat:@"%@",cityName];
    
    NSString * btnName = nil;
    if ([provinceName1 isEqualToString:cityName1]) {
        btnName = [NSString stringWithFormat:@"%@",provinceName1];
    }else{
        btnName = [NSString stringWithFormat:@"%@-%@",provinceName1,cityName1];
    }
    
    //    NSString * btnName = [NSString stringWithFormat:@"%@%@",provinceName,cityName];
    if ([_btnString isEqualToString:@"2"]) {
        
        [self.endButton setTitle:btnName forState:UIControlStateNormal];

    } else {
        
        [self.startButton setTitle:btnName forState:UIControlStateNormal];

    }
    UIImageView * image = (UIImageView*)[self.view viewWithTag:1000];
    image.transform = CGAffineTransformMakeRotation(0);
}

//  跳出的pickerview  显示列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}


//显示pickerview 每个视图的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    switch (component) {
        case 0:
            return _provienceArray.count;
            break;
        case 1:
            return _cityArray.count;
            break;
            //        case 2:
            //            return _areaArray.count;
            //            break;
        default:
            break;
    }
    return 0;
}

//显示每个pickerview  每行的具体内容

-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    
    
    UILabel *lable  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    lable.adjustsFontSizeToFitWidth = YES;
    lable.textAlignment = NSTextAlignmentCenter;
    
    switch (component) {
        case 0:{
            NSDictionary *dict = [_provienceArray objectAtIndex:row];
            lable.text = MySetValue(dict, @"provinceName");
        }
            
            break;
        case 1:{
            
            NSDictionary *dict = [_cityArray objectAtIndex:row];
            lable.text = MySetValue(dict, @"cityName");
        }
            
            break;
            
            //        case 2:{
            //
            ////            NSDictionary *dict = [_areaArray objectAtIndex:row];
            ////            lable.text = MySetValue(dict, @"countyName");
            //        }
            //
            //            break;
        default:
            break;
    }
    return lable;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
